package com.zidian.study.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zidian.student.trucks.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by GongCheng on 2016/11/24.
 */

public class ImagePikerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final static int TYPE_ITEM = 1;
    private final static int TYPE_FOOT = 2;

    private List<String> images;
    private Context context;
    private int imageCount = 4;

    public ImagePikerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_published_grida, parent, false);
            return new ItemViewHolder(view);
        }
        if (viewType == TYPE_FOOT) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_image_picker_foot, parent, false);
            return new FootViewHolder(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder,  int position) {
        if (holder instanceof ItemViewHolder) {

            Glide.with(context)
                    .load(images.get(position))
                    .into(((ItemViewHolder) holder).itemGridImage);

        }
        if (holder instanceof FootViewHolder) {
            ((FootViewHolder) holder).itemGridImage.setImageResource(R.mipmap.add_picture);
            if (position >= 1) {
                ((FootViewHolder) holder).addPhotoTv.setVisibility(View.GONE);
            }
            if (position >= imageCount) {
                ((FootViewHolder) holder).itemGridImage.setVisibility(View.GONE);
            } else {
                ((FootViewHolder) holder).itemGridImage.setVisibility(View.VISIBLE);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(holder.getAdapterPosition());
                }
            }
        });
    }

    /**
     * 设置图片数目
     * @param imageCount
     */
    public void setImageCount(int imageCount) {
        if (imageCount > 5) {
            this.imageCount = 5;
        }
        this.imageCount = imageCount;
    }

    public void removeItem(int position) {
        if (position < imageCount) {
            images.remove(position);
            notifyDataSetChanged();
        } else {
            return;
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<String> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return images == null ? 1 : images.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return TYPE_FOOT;
        } else {
            return TYPE_ITEM;
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_grid_image)
        ImageView itemGridImage;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_grid_image)
        ImageView itemGridImage;
        @Bind(R.id.add_photo_tv)
        TextView addPhotoTv;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
